package com.qzimp.fujiancuo.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qzimp.fujiancuo.entity.Admin;
import com.qzimp.fujiancuo.entity.AdminLog;
import com.qzimp.fujiancuo.entity.Userinfo;
import com.qzimp.fujiancuo.entity.UserinfoLog;
import com.qzimp.fujiancuo.entity.vo.AdminHaveLogsVo;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.entity.vo.UserHaveLogsVo;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.mapper.UserinfoMapper;
import com.qzimp.fujiancuo.service.UserinfoLogService;
import com.qzimp.fujiancuo.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author 吴宗波
 * @since 2024-11-21
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {
    @Autowired(required = false)
    private  UserinfoMapper userinfoMapper;
    @Autowired
    private UserinfoLogService userinfoLogService;
    @Override
    public Userinfo findUserinfoByLoginName(String loginName) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("login_name",loginName);

        return userinfoMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public Userinfo login(String loginName, String password) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("login_name",loginName);
        queryWrapper.eq("password",password);
        Userinfo userinfo= userinfoMapper.selectOneByQuery(queryWrapper);
        if(userinfo==null){
            throw  new ApiException(ResultCode.USER_LOGIN_NAME_NOT_EXIST);
        }
        //生成token

        final  String token= JWT.create()
                .setNotBefore(DateUtil.date())
                .setKey("abcdef".getBytes())
                .sign();
        userinfo.setToken(token);
        //更新token
        int ok= userinfoMapper.update(userinfo);
        if(ok<1){
            if(userinfo==null){
                throw  new ApiException(ResultCode.USER_TOKEN_UPDATE_FAILURE);
            }
        }

        return  userinfo;

    }

    @Override
    public UserHaveLogsVo queryUserHaveLogs(String loginName) {
        //获取管理员信息通过管理员账号

        Userinfo userinfo=this.queryUserinfoByName(loginName);

        if(userinfo==null){
            throw  new ApiException(ResultCode.CUSTOM_ERROR,"用户账户账户不存在");
        }

        // 通过管理员编号获取日志信息
        List<UserinfoLog> logList= userinfoLogService.queryUserinfoLogs(userinfo.getId());


        //把所有的信息放到包装类AdminHaveLogsVo
        UserHaveLogsVo userHaveLogsVo=new UserHaveLogsVo();

        userHaveLogsVo.setLoginName(loginName);
        userHaveLogsVo.setUserinfoLogList(logList);

        return userHaveLogsVo;
    }

    private Userinfo queryUserinfoByName(String loginName) {
        QueryWrapper queryWrapper=new QueryWrapper();

        queryWrapper.eq("login_name",loginName);

        return userinfoMapper.selectOneByQuery(queryWrapper);
    }


    public Userinfo authenticateUser(String username, String password) {
        return userinfoMapper.findUserByLoginNameAndPassword(username, password);
    }



    @Override
    public boolean updatePasswordById(Userinfo userinfo) {

        return userinfoMapper.updatePassword(userinfo.getId(),userinfo.getPassword());
    }

}
